package com.ruoyi.web.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.web.domain.ImputationRecord;
import com.ruoyi.web.enums.ImputationRecordEnum;
import com.ruoyi.web.service.IImputationRecordService;
import com.ruoyi.web.vo.ImputationRecordPageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 归集记录Controller
 *
 * @author ruoyi
 * @date 2023-01-06
 */
@RestController
@RequestMapping("/imputation/record")
public class ImputationRecordController extends BaseController {
    @Autowired
    private IImputationRecordService imputationRecordService;

    @PostMapping
    public R<String> create(@RequestBody ImputationRecord imputationRecord) {
        return imputationRecordService.create(imputationRecord);
    }

    /**
     * 查询归集记录列表
     */
    @PreAuthorize("@ss.hasPermi('web:record:list')")
    @GetMapping("/list")
    public TableDataInfo list(ImputationRecord imputationRecord) {
        startPage();
        List<ImputationRecord> list = imputationRecordService.selectImputationRecordList(imputationRecord);
        TableDataInfo info = getDataTable(list);

        List<ImputationRecordPageVO> voList = new ArrayList<>();
        for (ImputationRecord user : list) {
            ImputationRecordPageVO vo = new ImputationRecordPageVO();
            BeanUtils.copyProperties(user, vo);
            vo.setState(ImputationRecordEnum.Status.findName(vo.getState()));
            voList.add(vo);
        }
        return getDataTable(info, voList);
    }

    /**
     * 导出归集记录列表
     */
    @PreAuthorize("@ss.hasPermi('web:record:export')")
    @Log(title = "归集记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ImputationRecord imputationRecord) {
        List<ImputationRecord> list = imputationRecordService.selectImputationRecordList(imputationRecord);
        ExcelUtil<ImputationRecord> util = new ExcelUtil<ImputationRecord>(ImputationRecord.class);
        util.exportExcel(response, list, "归集记录数据");
    }

    /**
     * 获取归集记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('web:record:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(imputationRecordService.selectImputationRecordById(id));
    }

//    /**
//     * 新增归集记录
//     */
//    @PreAuthorize("@ss.hasPermi('web:record:add')")
//    @Log(title = "归集记录", businessType = BusinessType.INSERT)
//    @PostMapping
//    public AjaxResult add(@RequestBody ImputationRecord imputationRecord)
//    {
//        return toAjax(imputationRecordService.insertImputationRecord(imputationRecord));
//    }

    /**
     * 修改归集记录
     */
    @PreAuthorize("@ss.hasPermi('web:record:edit')")
    @Log(title = "归集记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ImputationRecord imputationRecord) {
        return toAjax(imputationRecordService.updateImputationRecord(imputationRecord));
    }

    /**
     * 删除归集记录
     */
    @PreAuthorize("@ss.hasPermi('web:record:remove')")
    @Log(title = "归集记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(imputationRecordService.deleteImputationRecordByIds(ids));
    }
}
